#include "KKComm.h"
#include "KKRenderI.h"
#include <Gdiplus.h>
#include <GdiplusHeaders.h>
#include <map>
#include <windows.h>

class KKRenderGdiPlus : public KKRenderI
{
  public:
    KKRenderGdiPlus();
    ~KKRenderGdiPlus();

  private:
    HDC m_hdc{nullptr};
    HWND m_hwnd{nullptr};
    Gdiplus::Pen *m_pen{nullptr};
    KKColor m_color{0, 0, 0, 255};
    Gdiplus::Graphics *m_graphics{nullptr};
    Gdiplus::LineCap m_line_cap{Gdiplus::LineCapRound};
    Gdiplus::LineJoin m_line_join{Gdiplus::LineJoinMiter};
    Gdiplus::DashStyle m_line_style{Gdiplus::DashStyleSolid};
    double m_line_width{1.0};
    Gdiplus::Font *m_font{nullptr};
    std::map<std::string, Gdiplus::Image *> m_image_cache{};
    std::map<std::string, Gdiplus::Font *> m_font_cache{};

    std::string _font_family_cur;
    int _font_size_cur;
    int _font_style_cur;

  public:
    bool Init(void *handle);
    void Release();
    bool IsReady();
    void Clear(KKColor color);
    void SetFont(std::string font_name, int font_size, int font_style);
    void SetColor(KKColor color);
    void SetLinearGradientColor(KKPoint start, KKPoint end, KKColor start_color, KKColor end_color);

    void SetLineStyle(int style, int arg1 = 0, int arg2 = 0);
    void SetLineWidth(double width);
    void SetLineCap(int cap);
    void SetLineJoin(int join);

    void DrawLine(KKPoint start, KKPoint end);
    void DrawRect(KKRect rect, bool is_fill = false);
    void DrawRoundRect(KKRect rect, int radius, bool is_fill = false);
    void DrawCircle(KKPoint center, int radius, bool is_fill = false);
    void DrawArc(KKRect rect, double start_angle, double sweep_angle);
    void DrawPolygon(std::vector<KKPoint> points, bool is_fill = false);

    void DrawText(std::string text, KKRect rect, KK_TEXT_ALIGN alignX = KK_TEXT_ALIGN_LEFT,
                  KK_TEXT_ALIGN alignY = KK_TEXT_ALIGN_CENTER); // 绘制utf8文本
    void DrawText(std::string text, KKPoint pos);               // 绘制utf8文本

    void DrawImage(std::string path, KKRect rect, KK_IMAGE_FIT fit = KK_IMAGE_FIT_FILL);
    void DrawImageBorder(std::string path, KKRect dest, KKSpace space);

    // util
    Gdiplus::Image *getImageCache(std::string path);
    Gdiplus::Font *getFontCache(std::string font_name, int font_size, int font_style);
    void DrawRect2(KKRect rect, KKSpace width, KKSpaceColor color, KKSpace line_style);

    // font
    std::string GetCurFontFamily();
    int GetCurFontSize();
    int GetCurFontStyle();
};